<?php
class controller_user extends base_module
{
    protected $User;

    public function __construct()
    {
        $this->User = RpcClient::instance("User");
        parent::__construct();
    }

    public function reg()
    {
        $data = $this->User->registerUser($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }

    #region 代理链接注册用户
    public function agentreg()
    {
        /*
        if(empty($this->paras['parentuno'])){
            echo json_encode(array(
               'state' => 404,
               'msgid' => 10080,
            ));
            return false;
        }
        */
        $paras = $this->paras;
        if($paras['usertype'] == 0){
            $data = $this->User->registerTry($paras);
            if($data['state']==200) {
                $result = $data['data'];
                echo json_encode($result);
            }else{
                echo json_encode($this->result);
            }
        }else{
            $data = $this->User->registerUser($this->paras);
            if($data['state']==200) {
                $result = $data['data'];
                echo json_encode($result);
            }else{
                echo json_encode($this->result);
            }
        }
    }
    #endregion

    # region 登录
    public function login(){
        $data = $this->User->userLogin($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 验证码

    public function verify($width=60,$height=24){
        $verifyCode = $this->User->createCode(3,6);
        $verifyCode = $verifyCode['data']['verify'];
        $width = intval($width);
        $height = intval($height);
        $image = imagecreatetruecolor(60, 24);
        //白色背景
        $white = imagecolorallocate($image, 255, 255, 255);
        //字体颜色
        $fontStyle = imagecolorallocate($image, mt_rand(0,255),mt_rand(0,255), mt_rand(0,255));;
        imagefill($image, 0, 0, $white);
        imagestring($image, 12, 3, 3, $verifyCode, $fontStyle);
        //加入干扰点
        for($i = 0; $i < 60; $i++) {
            $color = imagecolorallocate($image, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
            imagesetpixel($image, mt_rand(0,60), mt_rand(0,24), $color);
        }
        //干扰线
        for($i = 0; $i < 5; $i++) {
            $color = imagecolorallocate($image, intval(mt_rand(0,255)), intval(mt_rand(0,255)), intval(mt_rand(0,255)));
            imageline($image, mt_rand(0,60), mt_rand(0,24), mt_rand(0,60), mt_rand(0,24), $color);
        }
        # 记录REDIS
        # $this->redisSetEx($Redis, $key, $value, $expire = 86400);
        //输出图片
        //\Workerman\Protocols\Http::header("Content-type: image/PNG");
        imagepng($image);
        //释放资源
        imagedestroy($image);
    }
    # endregion

    #region 转送红包给玩家
    public function sendtousercoin()
    {
        $data = $this->User->sendToUserCoin($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            # Gateway::sendtouid($data['receiveruno'], json_encode($data['receiver']));
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 银行列表
    public function banklists(){
        $data = $this->User->bankLists($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion5

    #region 用户已设置的银行卡列表
    public function banks()
    {
        $data = $this->User->userbanks($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 用户银行卡管理
    public function setuserbank(){
        $data = $this->User->setUserBank($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    public function delbank()
    {
        $data = $this->User->delUserBank($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    public function editbank()
    {
        $data = $this->User->editUserBank($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    #region 提现
    public function cash()
    {
        $data = $this->User->userCash($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    #endregion

    # region 收货地址
    public function receivingaddresslist()
    {
        echo json_encode($this->User->receivingAddressList($this->paras));
    }
    public function receivingaddressdelete()
    {
        echo json_encode($this->User->receivingAddressDelete($this->paras));
    }
    public function receivingaddresseditsave()
    {
        echo json_encode($this->User->receivingAddressEditSave($this->paras));
    }
    public function receivingaddressaddsave()
    {
        echo json_encode($this->User->receivingAddressAddSave($this->paras));
    }
    public function receivingaddresssdefault()
    {
        echo json_encode($this->User->receivingAddressSetDefault($this->paras));
    }
    # endregion

    # region  资金帐变
    public function coinlog()
    {
        $data = $this->User->userCoinLog($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region  积分帐变
    public function scorelog()
    {
        $data = $this->User->userScoreLog($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region  游戏发包记录
    public function sendredenvelopeslog()
    {
        $data = $this->User->sendRedEnvelopesLog($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 已完成的单期游戏红包数据
    public function redenvelopesforissue()
    {
        $data = $this->User->RedEnvelopesForIssueItem($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region  抢包记录
    public function grabredenvelopeslog()
    {
        $data = $this->User->grabRedEnvelopesLog($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }

    # 创建房间记录
    public function createroomEnvelopesLog()
    {
        $data = $this->User->createroomEnvelopesLog($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }

    # endregion

    # region 玩家传包（转帐）记录
    public function transferlog()
    {
        $data = $this->User->transferLog($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 玩家收包（转帐）记录
    public function transfertomelog()
    {
        $data = $this->User->transferToMeLog($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 兑换日志
    public function exchangeorderslog()
    {
        echo json_encode($this->User->exchangeOrdersLog($this->paras));
    }
    #endregion

    # region 绑定手机号码
    public function setusermobile($paras)
    {
        $data = $this->User->setUserMobile($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 玩家昵称设置
    public function setnickname($paras)
    {
        $data = $this->User->updateUserNickname($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 玩家帐号设置
    public function setusername($paras)
    {
        $data = $this->User->updateUserName($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 玩家QQ设置
    public function setuserqq($paras)
    {
        $data = $this->User->updateUserQQ($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 玩家返点设置
    public function setuserrebate()
    {
        $data = $this->User->setUserRebate($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 玩家WX设置
    public function setuserwx($paras)
    {
        $data = $this->User->updateUserWX($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 玩家系统头像列表
    public function systemavatarlist(){
        $data = $this->User->systemAvatarList();
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 玩家头像设置
    public function updateavatar()
    {
        $data = $this->User->updteUserheader($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 更新用户登录密码
    public function updateuserpasswd()
    {
        $data = $this->User->updateUserPassword($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 充值记录
    public function rechargelog()
    {
        $data = $this->User->userRechargeLog($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 提现记录
    public function cashlog()
    {
        $data = $this->User->userCashLog($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 游戏数据统计
    public function statistics()
    {
        $data = $this->User->statisticsForUid($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 拉取站内信息标题
    public function usermaillist()
    {
        $data = $this->User->getUserMailList($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 拉取站内信息标题
    public function usermailitem()
    {
        $data = $this->User->getUserMailItem($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 拉取跑马灯内容
    public function marquee()
    {
        $data = $this->User->getMarquee($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 下级玩家
    public function userstatistics()
    {
        $data = $this->User->lowerLevelUserItem($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    public function lowerleveluser()
    {
        $data = $this->User->lowerLevelUser($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    // 所有代理等级
    public function agentlist()
    {
        $data = $this->User->agentLevelList($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    // 单个代理等级数据
    public function agentitem()
    {
        $data = $this->User->agentLevelForLid($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    // 当前用户是代理时返回统计数据
    public function agentteaminfo()
    {
        $data = $this->User->myTeamInfo($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }

    # region 发送手机验证码
    public function sendmobilemessage(){
        $data = $this->User->sendVerifyToMobile($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 按TOKEN拉取用户信息
    public function getuserinfo(){
        $data = $this->User->getUserInfoForToken($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            unset($result['uid']);

            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }
    # endregion

    # region 代理链接

    public function agentlinkslist(){
        $data = $this->User->agentLinksList($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }

    public function setlinkrebate(){
        $data = $this->User->agentLinksEdit($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }

    public function agentlinkdelete(){
        $data = $this->User->agentLinksDelete($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }

    public function agentlinkadd(){
        $data = $this->User->agentLinksAdd($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }

    # endregion
    public function createroom()
    {
        $data = $this->User->createRoom($this->paras);
        if($data['state']==200) {
            $result = $data['data'];
            echo json_encode($result);
        }else{
            echo json_encode($this->result);
        }
    }

    public function gufc()
    {
        global $utility;
        $data = $this->User->getUserCoin($this->paras);
        //print_r($data);
        $result = $data['data'];
        if(isset($result['online'])) {
            $public_online = \GatewayWorker\Lib\Gateway::getClientCountByGroup('public');
            $result['online'] = (intval($result['online']) + intval($public_online));
            echo json_encode($result);
        }
    }

    public function importauthsenduser()
    {
        $data = $this->User->importAuthendUser($this->paras);
    }
}